package dataStructure.stack;

import java.util.Scanner;

// 顺序栈的实现
public class SequenceStack {
    public static void main(String[] args) {
        // 初始化栈
        ArrayStack stack = new ArrayStack(4);
        String key = "";
        boolean loop = true; //
        Scanner scanner = new Scanner(System.in);

        while(loop) {
            System.out.println("show: 打印栈元素");
            System.out.println("exit: 离开");
            System.out.println("push: 入栈操作");
            System.out.println("pop: 出栈操作");
            System.out.println("请输入想进行的操作");
            key = scanner.next();
            switch (key) {
                case "show":
                    stack.display();
                    break;
                case "push":
                    System.out.println("请输入入栈元素");
                    int value = scanner.nextInt();
                    stack.push(value);
                    break;
                case "pop":
                    try {
                        int res = stack.pop();
                        System.out.printf("出栈元素是 %d\n", res);
                    } catch (Exception e) {
                        // TODO: handle exception
                        System.out.println(e.getMessage());
                    }
                    break;
                case "exit":
                    scanner.close();
                    loop = false;
                    break;
                default:
                    break;
            }
        }

        System.out.println("~~~~~~~~");
    }
}
// 顺序栈的实现
class ArrayStack  implements IStack{
    private int maxSize; // 栈的容量
    private int[] stack; // stack数组
    private int top = -1;// 栈顶指针

    //初始化栈
    public ArrayStack(int maxSize) {
        this.maxSize = maxSize;
        stack = new int[this.maxSize];
    }


    //满？
    @Override
    public boolean isFull() {
        return top == maxSize - 1;
    }
    //栈空？
    @Override
    public boolean isEmpty() {
        return top == -1;
    }
    //进栈
    @Override
    public void push(int value) {
        if(isFull()) {
            System.out.println("栈已满");
            return;
        }
        top++;
        stack[top] = value;
    }
    //出栈
    @Override
    public int pop() {
        if(isEmpty()) {
            throw new RuntimeException("栈空~");
        }
        int value = stack[top];
        top--;
        return value;
    }
    //取栈顶元素
    @Override
    public int peek() {
        if(isEmpty()) {
            throw new RuntimeException("栈空~");
        }
        int value = stack[top];
        // top--;
        return value;
    }
    // 打印栈元素
    @Override
    public void display() {
        if(isEmpty()) {
            System.out.println("栈空~~");
            return;
        }
        // 注意顺序
        for(int i = top; i >= 0 ; i--) {
            System.out.printf("stack[%d]=%d\n", i, stack[i]);
        }
    }
    @Override
    public void clear(){
        this.top=-1;
    }

    @Override
    public int length(){
        int length = top + 1 ;
        return length  ;
    }
}

interface IStack {

    void clear();       //清空方法
    boolean isEmpty();  //判空方法
    boolean isFull();   //判满方法
    int length();       //栈深度方法
    int peek();      //取栈顶元素并返回值，若栈为空，返回null
    int pop();       //将栈顶元素出栈  其实也是假删除  原素组元素里面还是有删除的元素的
    void push(int data);  //入栈操作,元素进入栈顶
    void display();
}